package cn.shop.admin.service.impl;

import cn.shop.admin.service.BackRateService;
import cn.shop.admin.service.MerchantRateService;
import cn.shop.base.MyServiceImpl;
import cn.shop.transcation.backRate.entity.BackRate;
import cn.shop.transcation.backRate.mapper.BackRateMapper;
import cn.shop.transcation.merchantRateModule.entity.MerchantRate;
import cn.shop.transcation.merchantUser.mapper.MerchantUserMapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 总后台 费率表(BackRate)表服务实现类
 *
 * @author 痴
 * @since 2022-05-12 14:19:46
 */
@Service("backRateService")
public class BackRateServiceImpl extends MyServiceImpl<BackRateMapper, BackRate> implements BackRateService {

	@Resource
	private MerchantUserMapper userMapper;

	@Resource
	private MerchantRateService merchantRateService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateWithMerchant(BackRate backRate) {
		// BackRate oldRate = baseMapper.selectById(backRate.getId());
		baseMapper.updateById(backRate);
		switch (backRate.getType()) {
			case "A":
				return updateWithMerchantForCurrency(backRate);
			case "B":
			case "C":
				return true;
			default:
				return false;
		}
	}

	public boolean updateWithMerchantForCurrency(BackRate backRate) {

		LambdaUpdateWrapper<MerchantRate> merchantRateUpdate = Wrappers.lambdaUpdate();
		merchantRateUpdate.
				set(MerchantRate::getRateType, backRate.getRateType()).
				set(MerchantRate::getSingleType, backRate.getSingleType()).
				set(MerchantRate::getChargeRate, backRate.getChargeRate()).
				set(MerchantRate::getChargeSingle, backRate.getChargeSingle()).
				set(MerchantRate::getTemplateFloat, backRate.getTemplateFloat());
		return merchantRateService.update(merchantRateUpdate);
	}

}

